<template>
  <view class="container-box">
    <!--搜索自愿活动-->
    <view class="search">
      <view class="serarch1" >
        <u-search  @search="seek" :showAction="false" :clearabled="true" focus placeholder="搜索自愿活动" v-model="keyword"></u-search>
      </view>
    </view>
    
    <!-- 活动分类栏 -->
      <view class="cotegory-box">
        <u-row
                 justify="space-between"
                 gutter="10"
         >
            <u-col span="2">
                <view class="category-item" @click="getAll">全部</view>
            </u-col>
             <u-col span="2">
                 <view class="category-item" @click="getActivityCategory('社区')">社区</view>
             </u-col>
             <u-col span="2">
                 <view class="category-item" @click="getActivityCategory('市政')">市政</view>
             </u-col>
             <u-col span="3">
                 <view class="category-item" @click="getActivityCategory('敬老院')">敬老院</view>
             </u-col>
             <u-col span="3">
                 <view class="category-item" @click="getActivityCategory('福利院')">福利院</view>
             </u-col>
         </u-row>
      </view>
    
    <!-- 活动内容部分 -->
    <view class="activity">
      <view class="item"  v-for="item in activityList" :key="item.id" @click="gotoDetail(item.activityId)">
        <!-- 活动的展示图 -->
        <view class="img">
          <view class="activity-text">
            {{item.title}}
          </view>
        </view>
        <!-- 活动的时间 -->
        <view class="text">
          <span>{{item.time}}</span>
        </view>
      </view>
    </view>    
  </view>
</template>

<script>
  export default {
    data() {
      return {
        keyword:'',
        activityList:[]
      };
    },
    onLoad(){
      // 获取所有的活动列表
      this.getActivityAll()
    },
    methods:{
      // 搜索活动处理函数
      seek(){
        this.getActivityCategory(this.keyword)
        this.keyword=''
      },
      // 获取所有活动信息
     async getActivityAll(){
        const {data:res} = await uni.$http.get('/activitys')
        if(res.code!==200) return uni.$showMsg(res.msg)
        this.activityList=res.data
      },
      // 获取全部的按钮
      getAll(){
        this.getActivityAll()
      },
      // 获取不同类型的活动
      async getActivityCategory(name){
        const {data:res} = await uni.$http.get('/activitys/like/'+name)
        if(res.code!==200) return uni.$showMsg("数据获取失败")
        this.activityList=res.data
      },
      // 进入活动的详情页面
      gotoDetail(activityId){
        // 跳转到活动的详情页面
        uni.navigateTo({ 
          // 将页面的参数传递给页面
          url:"/subpkg/activity-detail/activity-detail?id="+activityId,
        })
      }
    }
  }
</script>

<style lang="scss">
.container-box{
  padding: 10px;
  .search{
    .serarch1{
      width: 100%;
    }
  }
  .activity{
    display: flex;
    flex-wrap: wrap;
    margin-top: 5px;
   .item{
     margin: 5px;
     width: calc(46%);
     height: 100px;
     border: 1px;
     box-shadow: 1px 1px 1px 1px gray;
     display: flex;
     flex-direction: column;
     .img{
       width: 100%;
       height: 80%;
       // 背景
       background-image: url('https://img-qn.51miz.com/preview/element/00/01/05/28/E-1052800-57BA7D89.jpg');
       background-size: cover;
       display: flex;
       justify-content: center;
       align-items: center;
     .img-item{
      width: 100%;
      height: 100%;
      display: block;
      }
      .activity-text{
         white-space: nowrap;
         overflow: hidden;
         text-overflow: ellipsis;
         padding: 0 10px;
        font-weight:700;
        color: white;
      }
     }
     .text{
       width: 100%;
       font-size: 12px;
       display: flex;
       justify-content: center;
       align-items: center;
       background-color: lightcyan;
     }
   }
  }
  .cotegory-box{
    margin-top: 10px;
    .category-item{
      text-align: center;
      border: 1px solid white;
      box-shadow: 1px 1px 1px 1px black;
      height: 30px;
      line-height: 30px;
      border-radius: 3px;

    }
  }
}
</style>
